package com.wangsd.system.controller;

import com.gitee.fastmybatis.core.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.controller.AjaxResult;
import com.wangsd.common.controller.BaseController;
import com.wangsd.common.jwt.LoginUser;
import com.wangsd.common.jwt.TokenService;
import com.wangsd.common.utils.ServletUtils;
import com.wangsd.system.entity.Brand;
import com.wangsd.system.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业态Controller
 *
 * @author wsd
 * @date 2021-06-18
 */
@Api(tags = "业态管理")
@RestController
@RequestMapping("/system/brand")
public class BrandController extends BaseController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "查询业态列表(分页)")
    @GetMapping("/list")
    public AjaxResult<PageInfo<Brand>> list(Brand brand) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        Page page = PageHelper.startPage(ServletUtils.getParameterToInt("pageNum"), ServletUtils.getParameterToInt("pageSize"));
        List<Brand> list = brandService.selectBrandList(brand);
        return AjaxResult.success(getPageInfo(page, list));
    }

    @ApiOperation(value = "查询业态(全部)")
    @GetMapping("/all")
    public AjaxResult list() {
        List<Brand> list = brandService.selectBrandList(null);
        return AjaxResult.success(list);
    }


    @ApiOperation(value = "获取业态详细信息")
    @GetMapping("/{id}")
    public AjaxResult<Brand> getInfo(@PathVariable("id") Long id) {
        Brand brand = brandService.selectBrandById(id);
        return AjaxResult.success(brand);
    }

    @ApiOperation(value = "新增/修改业态")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody Brand brand) {
        if (brand.getId() == null){
            brandService.insertBrand(brand);
        } else {
            brandService.updateBrand(brand);
        }
        return AjaxResult.success("success");
    }


    @ApiOperation(value = "删除业态")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        brandService.deleteBrandById(id);
        return AjaxResult.success("success");
    }
}